package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/notices")  // 公告API路径前缀
@CrossOrigin  // 允许跨域访问
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")  // Swagger API分组
public class NoticeController {


    @Autowired
    private NoticeService noticeService;

    /*
    获取所有的公告
    * */
    @Operation(summary = "获取所有的公告")
    @GetMapping("/list")
    public Result<List<Notice>> getAllNotices() {
        return noticeService.getAllNotices();
    }

    /*
    根据id获取公告
    * */
    @Operation(summary = "根据id获取公告")
    @GetMapping("/{id}")
    public Result getNoticeByid(@PathVariable Long id) {
        Notice byId = noticeService.getById(id);
        return Result.success(byId);
    }

    /*
    添加公告
    * */
    @Operation(summary = "添加公告")
    @PostMapping("/add")
    public Result save(@RequestBody Notice notice) {
        Result<String> stringResult = noticeService.addNotice(notice);
        return Result.success(stringResult);
    }

    /*
    更新公告
    * */
    @Operation(summary = "更新公告")
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        Result<String> stringResult = noticeService.updateNotice(notice);
        return Result.success(stringResult);
    }

    /*
    根据id删除公告
    * */
    @Operation(summary = "根据id删除公告")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        Result<String> stringResult = noticeService.deleteNotice(id);
        return Result.success(stringResult);
    }

    /*
    获取启用的公告
    * */
    @Operation(summary = "获取启用的公告")
    @GetMapping("/activenotice")
    public Result activenotice() {
        Result<List<Notice>> activeNotices = noticeService.getActiveNotices();
        return Result.success(activeNotices);
    }

    /*
    获取最新的公告
     */
    @Operation(summary = "获取最新的公告")
    @GetMapping("/latest")
    public Result<List<Notice>> latest(@RequestParam int limit) {
        return noticeService.getLatestNotices(limit);

    }


    /*
    启用禁用公告
    * */
    @Operation(summary = "启用禁用公告")
    @PutMapping("/toggle/{id}")
    public Result<String> toggle(@PathVariable Long id, @RequestParam Boolean isActive) {
        return noticeService.toggleNoticeStatus(id, isActive);
    }
}



